package com.tiantianyuegou.model;

/**
 * Created by Administrator on 2016/1/26.
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.tiantianyuegou.R;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.trouble.framework.adapter.BaseAdapterHelper;
import com.trouble.framework.adapter.QuickAdapter;
import com.tiantianyuegou.R;
import com.tiantianyuegou.model.GoodsModel;

import java.util.ArrayList;
import java.util.List;


public class TuanGouGoodsAdapter extends BaseAdapter {
    private Context context;
    private List<TuanGouGoodsInfo> data;

    public TuanGouGoodsAdapter(Context context) {
        this.context = context;
        data=new ArrayList<>();
    }

    public TuanGouGoodsAdapter(Context context, List<TuanGouGoodsInfo> data) {
        this.context = context;
        this.data = data;
    }
    public void setData(List<TuanGouGoodsInfo> data){
        this.data.clear();
        this.data.addAll(data);
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View converView, ViewGroup parent) {
        TuanGouGoodsInfo model=data.get(position);
        final ViewHolder holder;
        if (converView==null){
            converView= LayoutInflater.from(context).inflate(R.layout.grid_item_tuijian, null);
            holder=new ViewHolder();
            holder.name= (TextView) converView.findViewById(R.id.tv_goodsname);
            holder.price= (TextView) converView.findViewById(R.id.tv_goodsprice);
            holder.redBag= (TextView) converView.findViewById(R.id.tv_redbagnum);
            holder.photo= (ImageView) converView.findViewById(R.id.iv_goods);
            converView.setTag(holder);
        }else{
            holder= (ViewHolder) converView.getTag();
        }
        holder.name.setText(model.getTpName()+"\n");
        Double price=Double.valueOf(model.getTpTuangouProce());
        holder.price.setText("¥"+new DecimalFormat("0.00").format(price));
        holder.redBag.setText(""+model.getTpRedCount());
//        ImageLoader.getInstance().init(
//                ImageLoaderConfiguration.createDefault(context));
        ImageLoader.getInstance().displayImage(model.getTpPic(),holder.photo);

        return converView;
    }
    class ViewHolder{
        TextView name;
        TextView price;
        TextView redBag;
        ImageView photo;
    }

}
